package org.jeecg.modules.zhouAdmin.admin.orderFactory.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.degression.pojo.MlccPaidanOrderGongyi;
import org.jeecg.modules.zhouAdmin.admin.orderCenterModule.pojo.MlccPaidanOrderAfter;
import org.jeecg.modules.zhouAdmin.admin.orderCenterModule.pojo.MlccPaidanOrderCost;
import org.jeecg.modules.zhouAdmin.admin.orderCenterModule.pojo.MlccSysuserOrderInfo;
import org.jeecg.modules.zhouAdmin.admin.orderFactory.service.IOrderFactoryService;
import org.jeecg.modules.zhoupackage.pojo.*;
import org.jeecg.modules.zhoupackage.pojo.myexcel.MyteamPriceProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/OrderFactoryController")
public class OrderFactoryController {

    @Autowired
    IOrderFactoryService service;

    /**
     * 获取供应商的个订(或团订)产品列表
     *
     * @param pageNo    当前页
     * @param pageSize  页面大小
     * @param orderSn   订单编号
     * @param time      派单时间
     * @param orderType 订单类型：0面料订单，1个订订单，2团订订单，3成衣订单
     */
    @RequestMapping("/listFactorySingleOrder")
    public Result<?> listFactorySingleOrder(@RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
                                            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                            String orderSn, String date, Integer orderType, String enddate) {
        Page<MlccSysuserOrderInfo> page = new Page<>(pageNo, pageSize);
        try {
            IPage<MlccSysuserOrderInfo> iPage = service.listFactorySingleOrder(page, orderSn, date, orderType, enddate);
            return Result.ok(iPage);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    /**
     * 获取产品的面料
     *
     * @param productId 产品id
     */
    @RequestMapping("/productFabric")
    public JSONObject productFabric(String productId) {
        JSONObject jsonObject = new JSONObject();
        try {
            List<MlccFabricInfo> list = service.productFabric(productId);
            jsonObject.put("code", 0);
            jsonObject.put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "操作失败");
        }
        return jsonObject;
    }

    /**
     * 加工厂获取产品的加价成本 (个订与团订通用)
     *
     * @param pageNo    当前页
     * @param pageSize  页面大小
     * @param productId 产品id
     */
    @RequestMapping("/listjaijaiChengbe")
    public Result<?> listjaijaiChengbe(@RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") int pageSize, String productId) {
        Page<MlccPaidanOrderCost> page = new Page<>(pageNo, pageSize);
        try {
            IPage<MlccPaidanOrderCost> jiajiaCB = service.listjaijaiChengbe(page, productId);
            return Result.ok(jiajiaCB);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    /**
     * 加工厂获取产品的工艺成本 (个订与团订通用)
     *
     * @param pageNo    当前页
     * @param pageSize  页面大小
     * @param productId 产品id
     */
    @RequestMapping("/getGYCB")
    public Result<?> getGYCB(@RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
                             @RequestParam(name = "pageSize", defaultValue = "10") int pageSize, String productId) {
        Page<MlccPaidanOrderGongyi> page = new Page<>(pageNo, pageSize);
        try {
            IPage<MlccPaidanOrderGongyi> jiajiaCB = service.getGYCB(page, productId);
            return Result.ok(jiajiaCB);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    /**
     * 加工厂获取产品的返修成本 (个订与团订通用)
     *
     * @param pageNo    当前页
     * @param pageSize  页面大小
     * @param productId 产品id
     */
    @RequestMapping("/getFanxiuCB")
    public Result<?> getFanxiuCB(@RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
                                 @RequestParam(name = "pageSize", defaultValue = "10") int pageSize, String productId) {
        Page<MlccPaidanOrderAfter> page = new Page<>(pageNo, pageSize);
        try {
            IPage<MlccPaidanOrderAfter> fanxiuCB = service.getFanxiuCB(page, productId);
            return Result.ok(fanxiuCB);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    /**
     * 获取团订下的所有产品（大产品）
     *
     * @param orderSn 订单编号
     */
    @RequestMapping("/listTeamPro")
    public JSONObject listTeamPro(String orderSn) {
        JSONObject jsonObject = new JSONObject();
        try {
            List<MyteamPriceProduct> productInfos = service.listTeamPro(orderSn);
            jsonObject.put("code", 0);
            jsonObject.put("data", productInfos);
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("data", "失败");
        }
        return jsonObject;
    }

    /**
     * 团订派单
     *
     * @param orderSn 订单编号
     */
    @RequestMapping("/teamPaidan")
    public JSONObject teamPaidan(String orderSn) {
        JSONObject jsonObject = new JSONObject();
        try {
            int result = service.teamPaidan(orderSn);
            if (result > 0) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "操作成功");
            } else if (result == -1) {
                jsonObject.put("code", -1);
                jsonObject.put("msg", "有产品没有选择供应商");
            } else {
                jsonObject.put("code", 1);
                jsonObject.put("msg", "操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("data", "操作失败");
        }
        return jsonObject;
    }

    /**
     * 团订-加工厂：大产品下小产品列表
     *
     * @param pageNo      当前页
     * @param pageSize    页面大小
     * @param productId   大产品id
     * @param productType 小产品类型 0团订，1标准码
     */
    @RequestMapping("/listSmallProByProduct")
    public Result<?> listSmallProByProduct(@RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                           String productId, Integer productType) {
        Page<MlccCustomerProductInfo> page = new Page<>(pageNo, pageSize);
        try {
            IPage<MlccCustomerProductInfo> product = service.listSmallProByProduct(page, productId, productType);
            return Result.ok(product);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("失败");
        }
    }

    /**
     * 加工厂小产品详情
     *
     * @param productId 产品ID
     * @param small     小产品id
     */
    @RequestMapping("/getDetail")
    public JSONObject getDetail(String productId, String small) {
        JSONObject jsonObject = new JSONObject();
        try {
            MlccProductInfo detail = service.getDetail(productId, small);
            jsonObject.put("code", 0);
            jsonObject.put("msg", "查询成功");
            jsonObject.put("data", detail);
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "查询失败");
        }
        return jsonObject;
    }

    /**
     * 团订获取人员尺寸
     *
     * @param proID 小产品id
     */
    @RequestMapping("/listSmallSize")
    public JSONObject listSmallSize(String proID) {
        JSONObject jsonObject = new JSONObject();
        try {
            List<MlccXiaoProductBodysize> size = service.listSmallSize(proID);
            jsonObject.put("code", 0);
            jsonObject.put("data", size);
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "查询失败");
        }
        return jsonObject;
    }

    /**
     * 根据小产品id获取部门详情
     *
     * @param smallproid 小产品id
     */
    @RequestMapping("/departmentDetailFromSmallPro")
    public JSONObject departmentDetailFromSmallPro(String smallproid) {
        JSONObject jsonObject = new JSONObject();
        try {
            MlccCustomerDepart depart = service.departmentDetailFromSmallPro(smallproid);
            jsonObject.put("code", 0);
            jsonObject.put("data", depart);
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "查询失败");
        }
        return jsonObject;
    }
}
